package beans;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlInputText;

import dao.DAO;
import dao.DAOFactory;

@ManagedBean
public class Frigo {

	DAO<Ingredient> ingredientDAO;
	public List<Ingredient> listeIngredients;
	private HtmlDataTable dataTable;
	private HtmlInputText quantite;
	
	public HtmlInputText getQuantite() {
		return quantite;
	}

	public void setQuantite(HtmlInputText quantite) {
		this.quantite = quantite;
		Ingredient aUpdate = (Ingredient) getDataTable().getRowData();
		ingredientDAO.update(aUpdate);
	}

	public Frigo () {
		ingredientDAO = DAOFactory.getDAOIngredient();
		load();
	}	

	public List<Ingredient> getListeIngredients() {
		return listeIngredients;
	}

	public void setListeIngredients(List<Ingredient> listeIngredients) {
		this.listeIngredients = listeIngredients;
	}
	
	public HtmlDataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(HtmlDataTable htmlDataTable) {
		this.dataTable = htmlDataTable;
	}
	
	public String nouvelIngredient() {
		ingredientDAO.create(new Ingredient("", 0f, 0));
		load();
		return null;
	}
	
	public String deleteIngredient () {
		Ingredient aSuppr = (Ingredient) getDataTable().getRowData();
		ingredientDAO.delete(aSuppr);
		listeIngredients.remove(aSuppr);
		return null;
	}
	
	public void load() {
		listeIngredients = ingredientDAO.findAll();
	}
	
	public void save() {	
		Ingredient aUpdate = (Ingredient) getDataTable().getRowData();
		ingredientDAO.update(aUpdate);					
	}
	
	
	
	
}
